//
//  Personalshare.m
//  DingDing
//
//  Created by LHL on 14/12/1.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "Personalshare.h"
#import "SDWEBIMAGE/UIImageView+WebCache.h"
@implementation Personalshare{
    UIView *contentView;
    UILabel *label;
    UIButton *argument;
    UIImageView *time_dot;
    UIView *timeline;
    UILabel *month;
    UILabel *days;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        time_dot = [[UIImageView alloc]init];
        time_dot.backgroundImage = [UIImage imageNamed:@"time_dot.png"];
        time_dot.frame = CGRectMake(37, 27, 6, 6);
        [self.contentView addSubview:time_dot];
        timeline = [[UIView alloc]initWithFrame:CGRectMake(40, 0, 1, self.frame.size.height)];
        timeline.backgroundImage = [UIImage imageNamed:@"time_line.png"];
        [self.contentView addSubview:timeline];
        contentView = [[UIView alloc]init];
//        contentView.backgroundImage = [[UIImage imageNamed:@"personal.png"]stretchableImageWithLeftCapWidth:6 topCapHeight:16];
        contentView.backgroundImage = [UIImage imageNamed:@"bakcground.png"];
        UIImageView *corner = [[UIImageView alloc]initWithFrame:CGRectMake(47, 25, 7, 14)];
        days = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, 0.125*[UIScreen mainScreen].bounds.size.width, 20)];
        days.font = [UIFont systemFontOfSize:20];
        days.textColor = RGB(85, 85, 85);
        days.textAlignment = NSTextAlignmentCenter;
        month = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, 0.125*[UIScreen mainScreen].bounds.size.width, 20)];
        month.font = [UIFont systemFontOfSize:11];
        month.textColor = RGB(85, 85, 85);
        month.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:month];
        [self.contentView addSubview:days];
        corner.image = [UIImage imageNamed:@"corner.png"];
        [self.contentView addSubview:corner];
        [self.contentView addSubview:contentView];
        for (int i = 0; i < 9; i++) {
            UIImageView *v = [[UIImageView alloc]init];
            v.tag = 10110+i;
            v.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImg:)];
            [v addGestureRecognizer:tap];
            [contentView addSubview:v];
        }
        label = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, self.superview.frame.size.width-10, 30)];
        label.numberOfLines = 2;
        label.lineBreakMode = NSLineBreakByTruncatingTail;
        label.font = [UIFont systemFontOfSize:12];
        label.textColor = RGB(121, 108, 104);
        [contentView addSubview:label];
        argument = [UIButton buttonWithType:UIButtonTypeCustom];

        [argument setBackgroundImage:[UIImage imageNamed:@"message.png"] forState:0];
//        [contentView addSubview:argument];
        self.contentView.clipsToBounds = YES;
        
    }
    return self;
}

-(void)clickImg:(UITapGestureRecognizer *)sender{
    UIImageView *imgview = (UIImageView *)sender.view;
    if (self.block) {
        self.block(imgview.tag%10110);
    }
    
}
-(void)getClickImage:(integerTrans)block{
    self.block = [block copy];
}

-(void)rebuildFrame:(SHAREITEMMODEL *)model{
    self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [Common getItemHeight:model]+10);
    contentView.frame = CGRectMake(54, 10, [UIScreen mainScreen].bounds.size.width*0.75, [Common getItemHeight:model]);
    CGFloat total = 0;

    if (model.image.count >1) {
        for (int i = 0; i < 9; i++) {
            UIImageView *v = (UIImageView *)[contentView viewWithTag:10110+i];
            if (i < model.image.count) {
                IMAGEMODEL *img = model.image[i];
                [v setImageWithURL:[NSURL URLWithString:img.smallimg]];
                v.frame = CGRectMake(10+(i%3)*80, 10+80*(i/3), 70, 70);
            }else{
                v.frame = CGRectZero;
            }
        }
        if (model.image.count >3) {
            if (model.image.count %3 == 0) {
                total +=  model.image.count/3*80;
            }else{
                total += (model.image.count/3 +1)*80;
            }
        }else{
            total += 1 *80;
        }
    }else{
        for (int i = 0; i < 9; i++) {
            UIImageView *v = (UIImageView *)[contentView viewWithTag:10110+i];
            if (i == 0) {
                IMAGEMODEL *img = model.image[i];
                [v setImageWithURL:[NSURL URLWithString:img.smallimg]];
                
                 v.frame = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width*0.6875, [UIScreen mainScreen].bounds.size.width*0.6875);
                total += [UIScreen mainScreen].bounds.size.width*0.6875;
                }else{
                v.frame = CGRectZero;
            }
        }
    }
    label.text = model.summary;
    if (model.image.count >0) {
        if (model.summary == nil || [model.summary isEqualToString:@""]) {
            label.frame = CGRectZero;
//            argument.frame = CGRectMake(160+21+18, total+20, 21, 18);
        }else{
            label.frame = CGRectMake(10, total+10, 220, 40);
//            argument.frame = CGRectMake(160+21+18, total+50, 21, 18);
        }
    }else{
        if (model.summary == nil || [model.summary isEqualToString:@""]) {
            label.frame = CGRectZero;
//            argument.frame = CGRectMake(160+21+18, 0, 21, 18);
        }else{
            label.frame = CGRectMake(10, 0, 220, 40);
//            argument.frame = CGRectMake(160+21+18, 40, 21, 18);
        }
    }
    timeline.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*0.125, 0, 1, total+52);
    NSDate  *date = [NSDate dateWithTimeIntervalSince1970:[model.create_time integerValue]];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
//    NSLog(@"enddate=%@",localeDate);
    NSString *datestr = [[NSString stringWithFormat:@"%@",localeDate] stringByReplacingOccurrencesOfString:@" " withString:@"-"];
    if (!(datestr == nil || [datestr isEqualToString:@""])) {
        NSArray *temp = [datestr componentsSeparatedByString:@"-"];
        month.text = [NSString stringWithFormat:@"%@月",temp[1]];
        days.text = temp[2];
    }
    
}

@end
